/**
 *
 * Copyright 2015 RSVP Technologies Inc. All rights reserved.
 * LineSearch.java
 *
 */
package com.gfinal.map.bus;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.gfinal.map.common.Constant;
import com.gfinal.map.util.HttpUtils;

/**
 * @author He Tao (ht@nbicc.com)
 * @date 2015年2月28日
 */
public class LineSearch {
	public static final String TAG = LineSearch.class.getName();
	private String city = null;// 城市
	private String q = null;// 关键字 如：466、1路
	private int result_num = 0;
	private String web_url = "http://bus.aibang.com";
	private String wap_url = "http://wap.aibang.com";
	/**
	 * 默认为0，不包含各个站点和路线的坐标信息，如果为1，则包含
	 */
	private int with_xys = 0;// 是否包含坐标点信息

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getQ() {
		return q;
	}

	public void setQ(String q) {
		this.q = q;
	}

	public int getWith_xys() {
		return with_xys;
	}

	public void setWith_xys(int with_xys) {
		this.with_xys = with_xys;
	}

	public int getResult_num() {
		return result_num;
	}

	public String getWeb_url() {
		return web_url;
	}

	public String getWap_url() {
		return wap_url;
	}

	@SuppressWarnings({ "unused", "rawtypes", "unchecked" })
	private String createUrl() {
		ArrayList params = new ArrayList();
		params.add(new BasicNameValuePair("app_key", Constant.AIBANG_AK));
		params.add(new BasicNameValuePair("city", city));
		params.add(new BasicNameValuePair("q", q));
		params.add(new BasicNameValuePair("with_xys", String.valueOf(with_xys)));
		return Constant.BUS_LINE_URL + "?"
				+ URLEncodedUtils.format(params, Constant.CHARSET);
	}

	@SuppressWarnings("rawtypes")
	public ArrayList<LineBean> execute() {
		ArrayList<LineBean> lineList = new ArrayList<LineBean>();
		String response = HttpUtils.doGet(createUrl());
		SAXReader reader = new SAXReader();

		Document doc;
		try {
			ByteArrayInputStream in = new ByteArrayInputStream(
					response.getBytes("UTF-8"));
			doc = reader.read(in);
			Element root = doc.getRootElement(); // 获取根节点
			List<Element> list = new ArrayList<Element>();
			list.add(root);
			while (list.size()>0) {
				Element element = null;
				Element ele = null;
				Iterator ite = list.iterator();
				if (ite.hasNext()) {
					ele = (Element) ite.next();
					ite.remove();
				}
				if (ele != null) {
					LineBean lineBean = null;
					for (Iterator i = ele.elementIterator(); (i != null)
							&& (i.hasNext());) {
						element = (Element) i.next();
						list.add(element);
						if (element != null) {
							 //System.out.println(element.getName() + " --" + element.getText());
							 if(element.getName().equals("result_num")){
								 result_num=Integer.valueOf(element.getText());
							 }
							 else if (element.getName().equals("name")) {
								lineBean = new LineBean();
								lineBean.setName(element.getText());
							} else if (element.getName().equals("info")) {
								lineBean.setInfo(element.getText());
							} else if (element.getName().equals("stats")) {
								lineBean.setStats(element.getText());
							} else if (element.getName().equals("stat_xys")) {
								lineBean.setStat_xys(element.getText());
							} else if (element.getName().equals("xys")) {
								lineBean.setXys(element.getText());
								lineList.add(lineBean);
							}
						}
					}
				}
				
				if(lineList.size()==result_num){
					break;
				}
			
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return lineList;

	}

	public static void main(String[] args) {
		LineSearch lineSearch = new LineSearch();
		lineSearch.setCity("鄞州");
		lineSearch.setQ("528路");
		lineSearch.setWith_xys(0);
		ArrayList<LineBean> list = lineSearch.execute();
		System.out.println(list);
	}

}
